import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';

class StateMenagement extends StatefulWidget {
  @override
  _StateManagementState createState() => new _StateManagementState();
}

class _StateManagementState extends State<StateMenagement> {

  // 状态数据
  int _count = 0;

  void _increaseCount() {
    setState(() {
      _count += 1;
    });
  }

  @override
  Widget build(BuildContext context) {
    return CounterProvider(
      count: _count,
      increaseCount: _increaseCount,
      child: Scaffold(
        appBar: AppBar(
          title: Text("StateMenagement"),
          elevation: 0.0,
        ),
        body: CounterWrapper(),
        floatingActionButton: FloatingActionButton(
          child: Icon(Icons.add),
          onPressed: _increaseCount,
        ),
      ),
    );
  }
}

class CounterModel extends Model {
  int _count = 0;
  int get count => _count;

  void increaseCount() {
    _count += 1;
    notifyListeners(); // 重构小部件
  }
}

// 小部件父子组件之间传值-InheritedWidget
class CounterProvider extends InheritedWidget {
  final int count;
  final VoidCallback increaseCount;
  final Widget child;

  CounterProvider({ 
    this.count, 
    this.increaseCount, 
    this.child 
  }):super(child: child);

  static CounterProvider of(BuildContext context) => 
    context.dependOnInheritedWidgetOfExactType();

  @override
  bool updateShouldNotify(covariant InheritedWidget oldWidget) {
    return true;
  }
}

class CounterWrapper extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Counter(),
    );
  }
}

class Counter extends StatelessWidget {

  @override
  Widget build(BuildContext context) {

    final int count = CounterProvider.of(context).count;
    final VoidCallback increaseCount = CounterProvider.of(context).increaseCount;

    return ActionChip(
      label: Text("$count"),
      onPressed: increaseCount,
    );
  }
}

// 小部件父子组件之间传值-使用ScopedModel
class StatelessMenagementDemo extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return ScopedModel(
      model: CounterModel(),
      child: Scaffold(
        appBar: AppBar(
          title: Text("StatelessMenagementDemo"),
          elevation: 0.0,
        ),
        body: CounterWrapperWithModel(),
        floatingActionButton: ScopedModelDescendant<CounterModel>(
          rebuildOnChange: false, // 不需要重新渲染小部件
          builder: (context, _, model) => FloatingActionButton(
            child: Icon(Icons.add),
            onPressed: model.increaseCount,
        ),
        ),
      ),
    );
  }
}

class CounterWrapperWithModel extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Center(
      child: CounterWithModel(),
    );
  }
}

class CounterWithModel extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return ScopedModelDescendant<CounterModel>(
        builder: (context, _, model) =>  ActionChip(
        label: Text("${model.count}"),
        onPressed: model.increaseCount,
      ),
    );
  }
}
